package com.caigou.controller;


import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.Video;
import com.caigou.bean.cms.entity.VideoFolder;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.VideoService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/video/")
public class VideoController {

    @Resource
    private VideoService videoService;


    /**
     * 获取视频根文件夹
     * @param user
     * @return
     */
    @RequestMapping("getRootVideoFolder")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result<VideoFolder> getRootVideoFolder(@CurrentUser Users user){
        VideoFolder videoFolder = videoService.getRootVideoFolder();
        return Result.success(videoFolder);
    }

    /**
     * 创建视频文件夹
     * @param videoFolderParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "创建视频库文件夹")
    @RequestMapping("createVideoFolder")
    @PreAuthorize("hasAnyAuthority('shipin_muluguanli')")
    public Result createVideoFolder(@RequestBody VideoFolderParam videoFolderParam, @CurrentUser Users user){
        if(videoFolderParam.getSuperId()==null || videoFolderParam.getFolderName()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(videoFolderParam.getFolderName().length()>50){
            return Result.error("视频目录名称不能超过50个字", "视频目录名称不能超过50个字");
        }
        return videoService.createVideoFolder(videoFolderParam.getSuperId(), videoFolderParam.getFolderName(), user.getUserid());
    }

    /**
     * 删除视频文件夹
     * @param videoFolderParam
     * @param user
     * @return
     */
    @RequestMapping("deleteVideoFolder")
    @PreAuthorize("hasAnyAuthority('shipin_muluguanli')")
    public Result deleteVideoFolder(@RequestBody VideoFolderParam videoFolderParam, @CurrentUser Users user){
        return videoService.deleteVideoFolder(videoFolderParam.getId());
    }

    /**
     * 删除视频文件夹
     * @param videoFolderParam
     * @param user
     * @return
     */
    @RequestMapping("updateVideoDefaultFolder")
    @PreAuthorize("hasAnyAuthority('shipin_muluguanli')")
    public Result updateVideoDefaultFolder(@RequestBody VideoFolderParam videoFolderParam, @CurrentUser Users user){
        return videoService.updateVideoDefaultFolder(videoFolderParam);
    }

    /**
     * 列表某一视频文件夹下的所有子文件夹列表
     * @param videoFolderParam
     * @param user
     * @return
     */
    @RequestMapping("listVideoFolderBySuperId")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result listVideoFolderBySuperId(@RequestBody VideoFolderParam videoFolderParam, @CurrentUser Users user){
        List<VideoFolder> videoFolderList = videoService.listVideoFolderBySuperId(videoFolderParam.getSuperId());
        return Result.success(videoFolderList);
    }

    /**
     * 通过视频名称查询视频列表，支持分页
     * @param videoQueryParam
     * @param user
     * @return
     */
    @RequestMapping("queryVideo")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result queryVideo(@RequestBody VideoQueryParam videoQueryParam, @CurrentUser Users user){
        PageInfo<Video> pageInfoVideo = videoService.queryVideoIncludingSubfolders(videoQueryParam, user);//videoService.queryVideoIncludingSubfolders(folderId, keyword, pageNum, pageSize);
        return Result.success(pageInfoVideo);
    }

    /**
     * 上传视频文件（支持多文件上传）
     * @param files
     * @param folderId
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "上传视频到视频库")
    @RequestMapping("uploadVideo")
    @PreAuthorize("hasAnyAuthority('shangchuanshipin')")
    public Result uploadVideo(@RequestParam(value="files") MultipartFile[] files, @RequestParam(value="videoNotes", required=false)String[] videoNotes,
                              @RequestParam(value="videoImages", required=false)String[] videoImages,
                              @RequestParam(value="folderId", required=false)Integer folderId, @RequestParam(value="videoSource", required=false)String videoSource,
                              @RequestParam(value="videoEditor", required=false)String videoEditor, @CurrentUser Users user){
        log.info("-----controller start-----");
        for(MultipartFile multipartFile:files){
            if(multipartFile.getOriginalFilename()==null || multipartFile.getOriginalFilename().length()<=4){
                return Result.error("视频名称不能为空", "视频名称不能为空");
            }
            if(multipartFile.getOriginalFilename().length()>34){
                return Result.error("视频名称超长，不能多于30个字符", "视频名称超长，不能多于30个字符");
            }
        }
        for(String note:videoNotes){
            if(note!=null && note.length()>100){
                return Result.error("视频备注超长，不能多于100个字符", "视频备注超长，不能多于100个字符");
            }
        }
        if(videoSource!=null && videoSource.length()>64){
            return Result.error("视频来源超长，不能多于64个字符", "视频来源超长，不能多于64个字符");
        }

        if(videoEditor!=null && videoEditor.length()>16){
            return Result.error("摄影编辑超长，不能多于16个字符", "摄影编辑超长，不能多于16个字符");
        }
        return videoService.uploadVideo(files, videoNotes, videoImages, folderId, videoSource, videoEditor, user.getUserid());
    }

    /**
     * 修改视频
     * @param videoParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改视频属性")
    @RequestMapping("updateVideo")
    @PreAuthorize("hasAnyAuthority('xiugaishipinxinxi')")
    public Result updateVideo(@RequestBody VideoParam videoParam, @CurrentUser Users user){
        if(videoParam.getId()==null || (videoParam.getVideoName()==null && videoParam.getVideoSource()==null && videoParam.getVideoEditor()==null
                && videoParam.getVideoNote()==null && videoParam.getVideoImage()==null)){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(videoParam.getVideoName()!=null && videoParam.getVideoName().length()>34){
            return Result.error("视频名称超长，不能多于30个字符", "视频名称超长，不能多于30个字符");
        }
        if(videoParam.getVideoNote()!=null && videoParam.getVideoNote().length()>100){
            return Result.error("视频备注超长，不能多于100个字符", "视频备注超长，不能多于100个字符");
        }
        if(videoParam.getVideoSource()!=null && videoParam.getVideoSource().length()>64){
            return Result.error("视频备注来源超长，不能多于64个字符", "视频备注来源超长，不能多于64个字符");
        }
        if(videoParam.getVideoEditor()!=null && videoParam.getVideoEditor().length()>16){
            return Result.error("视频编辑超长，不能多于16个字符", "视频编辑超长，不能多于16个字符");
        }
        return videoService.updateVideo(videoParam, user);
    }

    /**
     * 修改视频名称
     * @param videoParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改视频名称")
    @RequestMapping("updateVideoName")
    @PreAuthorize("hasAnyAuthority('xiugaishipinxinxi')")
    public Result updateVideoName(@RequestBody VideoParam videoParam, @CurrentUser Users user){
        if(videoParam.getId()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(videoParam.getVideoName()==null||videoParam.getVideoName().length()<=4){
            return Result.error("视频名称不能为空", "视频名称不能为空");
        }
        if(videoParam.getVideoName()!=null && videoParam.getVideoName().length()>34){
            return Result.error("视频名称超长，不能多于30个字符", "视频名称超长，不能多于30个字符");
        }
        return videoService.updateVideoName(videoParam.getId(), videoParam.getVideoName(), user.getUserid());
    }

    /**
     * 删除视频文件:逻辑删除
     * @param videoBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除视频")
    @PreAuthorize("hasAnyAuthority('shanchushipin')")
    @RequestMapping("deleteVideo2Recycle")
    public Result deleteVideo2Recycle(@RequestBody VideoBatchParam videoBatchParam, @CurrentUser Users user){
        if(videoBatchParam.getIds()==null||videoBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return videoService.deleteVideo2Recycle(videoBatchParam.getIds());
    }

//    /**
//     * 更新视频文件使用状态
//     * @param videoParam
//     * @param user
//     * @return
//     */
//    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改视频使用状态")
//    @RequestMapping("updateVideoIsUse")
//    public Result updateVideoIsUse(@RequestBody VideoParam videoParam, @CurrentUser Users user){
//        if(user==null){
//            log.warn("用户未认证登录请求/video/updateVideoIsUse接口");
//            return Result.error(ResponseEnum.NO_TOKEN);
//        }
//        if(videoParam.getId()==null||videoParam.getVideoIsUse()==null){
//            return Result.error(ResponseEnum.PARAM_ERROR);
//        }
//        return videoService.updateVideoIsUse(videoParam.getId(), videoParam.getVideoIsUse(), user.getUserid());
//    }

    @RequestMapping("getVideoTotalSize")
    public Result getVideoTotalSize(@CurrentUser Users user){
        return videoService.getVideoTotalSize();
    }

    @RequestMapping("useVideo")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result useAudio(@RequestBody VideoUseParam videoUseParam, @CurrentUser Users user){
        return videoService.useVideo(videoUseParam);
    }

    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动视频")
    @RequestMapping("moveVideo")
    @PreAuthorize("hasAnyAuthority('yidongshipin')")
    public Result moveVideo(@RequestBody VideoBatchParam videoBatchParam, @CurrentUser Users user){
        if(videoBatchParam.getIds().length==0){
            return Result.error("缺失视频id参数");
        }
        return videoService.moveVideo(videoBatchParam.getIds(), videoBatchParam.getTargetFolderId());
    }

    @RequestMapping("queryVideoInRecycle")
    @PreAuthorize("hasAnyAuthority('shipin_quankuchaxun')")
    public Result queryVideoInRecycle(@RequestBody VideoRecycleQueryParam videoRecycleQueryParam, @CurrentUser Users user){
        PageInfo<Video> videoPageInfo = videoService.queryVideoInRecycle(videoRecycleQueryParam);
        return Result.success(videoPageInfo);
    }

    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "从回收箱恢复视频")
    @RequestMapping("restoreVideo")
    @PreAuthorize("hasAnyAuthority('shipinkuhuishouzhanguanli')")
    public Result restoreVideo(@RequestBody VideoBatchParam videoBatchParam, @CurrentUser Users user){
        if(videoBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return videoService.restoreVideo(videoBatchParam.getIds());
    }

    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "从回收箱彻底删除视频")
    @RequestMapping("deleteVideoFromRecycle")
    @PreAuthorize("hasAnyAuthority('shipinkuhuishouzhanguanli')")
    public Result deleteVideoFromRecycle(@RequestBody VideoBatchParam videoBatchParam, @CurrentUser Users user){
        if(videoBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return videoService.deleteVideoFromRecycle(videoBatchParam.getIds());
    }

    @OperLog(operModule = NewsConstant.VIDEO_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "清空视频回收站")
    @RequestMapping("clearRecycle")
    @PreAuthorize("hasAnyAuthority('shipinkuhuishouzhanguanli')")
    public Result clearRecycle(@CurrentUser Users user){
        return videoService.clearRecycle();
    }
}
